<?php


namespace App\Models\CacheModel\lucky;


use app\common\lib\exception\ApiException;
use App\Models\Api\BaseCache;
use think\Env;

class AppLuckyExchange extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_lucky_exchange';

    protected $cacheModel = 'lucky_planet.exchange_datas';
    protected $starImg = 'https://oss.goddessxzns.com/test/lucky_star/star.png';

    /**
     * 获取 装扮兑换 列表
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getExchangeLists(int $uid)
    {
        request()->get(['page' => -1]);
        $oss = Env::get('host.photo');
        $datas = $this->getListPage('', 'sort');
        $UserAdornInfoModel = new AppUserAdornInfo();
        // 用户已有装扮
        $userAdronDatas = $UserAdornInfoModel->getUserAdorns($uid);
        $userAdronDatas = array_column($userAdronDatas, null, 'exchange_id');
        // 获取用户当前装扮
        $userAdronConfig = $UserAdornInfoModel->getUserAdornConfig($uid);
        $useIds = []; // 装扮的iDS
        if ($userAdronConfig)
        {
            $userAdronConfig['adorn_info'] = json_decode($userAdronConfig['adorn_info'], true);
            $userAdronConfig['tag_info'] = json_decode($userAdronConfig['tag_info'], true);
            $userAdronConfig['badge_info'] = json_decode($userAdronConfig['badge_info'], true);

            $useIds = [$userAdronConfig['adorn_info']['id'], $userAdronConfig['tag_info']['id']];
            if ($userAdronConfig['badge_info'])
            {
                $useIds = array_merge($useIds, array_column($userAdronConfig['badge_info'], 'id'));
            }
        }


        // 装扮信息
        $adornDatas = [];
        foreach ($datas['data'] as $dataItem)
        {
            $dataItem['photo'] = json_decode($dataItem['photo'], true);

            $dataItem = arrayKeyData2int($dataItem, ['id', 'status', 'sort', 'type', 'lucky_num', 'days', 'create_time']);

            if (isset($userAdronDatas[$dataItem['id']]))
            {
                if (in_array($userAdronDatas[$dataItem['id']]['id'], $useIds, false))
                {
                    $status = 2;
                    $btnText = '装扮中';
                } else
                {
                    $status = 1;
                    $btnText = '去装扮';
                }
                $des = '到期时间：' . date('Y.m.d H:i', $userAdronDatas[$dataItem['id']]['expire_time']);
            } else
            {
                $status = 0;
                $btnText = '兑换';
                $des = '幸运星 x' . $dataItem['lucky_num'];
            }


            $adornDatas[$dataItem['type']][] = [
                'id' => $dataItem['id'],
                'title' => $dataItem['name'] . $dataItem['days'] . '天',
                'adorn' => $oss . $dataItem['photo']['img_list']['url'],
                'des' => $des,
                'img' => $this->starImg,
                'btn_text' => $btnText,
                'btn_tip' => '查看效果',
                'status' => $status,
                'cost' => $dataItem['lucky_num'],
                'preview_img' => $oss . $dataItem['photo']['img_preview']['url'],
                'reality_img' => $oss . $dataItem['photo']['img']['url'],
            ];
        }

        return $adornDatas;
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @throws ApiException
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $where = [];
            if ($ids)
            {
                $where = ['id' => ['in', $ids]];
            }

            $data = $this->where($where)->order('sort ' . $sortType)->column('status,sort,type,name,des,lucky_num,days,photo,create_time', 'id');

            return $data;
        }
        catch (\Exception $e)
        {
            return [];
        }
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $datas = parent::batchGetInfoByIds($cids, $field);

        foreach ($datas as $id => $dataItem)
        {
            if (isset($dataItem['photo']))
            {
                $dataItem['photo'] = json_decode($dataItem['photo'], true);
            }

            $datas[$id] = arrayKeyData2int($dataItem, ['id', 'status', 'sort', 'type', 'lucky_num', 'days', 'create_time']);
        }

        return $datas;
    }
}
